package com.taritari.journal.controller;

import cn.hutool.core.bean.BeanUtil;
import com.taritari.journal.entity.dto.JournalInfoDto;
import com.taritari.journal.entity.dto.RegisterDto;
import com.taritari.journal.entity.dto.UserDTO;
import com.taritari.journal.entity.po.JournalUser;
import com.taritari.journal.services.UserService;
import com.taritari.journal.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author taritari
 * @date 2023-12-04 21:40
 * @description
 */
@RestController
@Slf4j
@Tag(name = "用户类Controller", description = "用户的一些功能，包括登录注册")
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    @Operation(summary = "登录", description = "登录")
    public Result login(@RequestBody UserDTO userDTO) throws Exception {
        Result login = userService.login(userDTO);
        return login;
    }

    @GetMapping("/info")
    @Operation(summary = "拦截器提醒", description = "info")
    public Result info(){

        return Result.success("你还没有登录哦");
    }

    @GetMapping("/getIdByToken")
    @Operation(summary = "通过token从redis获取id", description = "通过token从redis获取id")
    public Result tokenGetId(@RequestParam String token){
        Integer result = userService.tokenGetUserId(token);
        return Result.success(result);
    }

    @GetMapping("/getCount")
    @Operation(summary = "获取近6个月的写作和阅读统计", description = "获取近6个月的写作和阅读统计")
    public Result getCount(@RequestParam int id){
        Map<String, Object> countById = userService.getCountById(id);
        return Result.success(countById);
    }


    @PostMapping("/register")
    @Operation(summary = "注册", description = "注册")
    public Result register(@RequestBody RegisterDto registerDto) throws Exception {
        Result result = userService.userRegister(registerDto);
        return result;
    }

    @GetMapping("/getUserInfoById")
    @Operation(summary = "通过id查询用户信息", description = "通过id查询用户信息")
    public Result getUserInfoById(@RequestParam int id){
        JournalUser journalUser = userService.queryUserInfoById(id);
        if (BeanUtil.isNotEmpty(journalUser)){
            return Result.success(journalUser);
        }else {
            return Result.success();
        }
    }

    @GetMapping("/getNumberCount")
    @Operation(summary = "通过id查询用户日记文本统计", description = "通过id查询用户日记文本统计")
    public Result getNumberCount(@RequestParam int id){
        Result result = userService.journalNumberCountById(id);
        return result;

    }

    @PostMapping("/update")
    @Operation(summary = "更改用户信息", description = "更改用户信息")
    public Result update(@RequestBody JournalInfoDto journalInfoDto){
        int res = userService.updateUserInfoById(journalInfoDto);
        return Result.success(res);
    }


    @GetMapping("/verifyRedis")
    @Operation(summary = "验证redis的value", description = "验证redis的value")
    public Result verifyRedis(@RequestParam String key,@RequestParam String value){
        Result result = userService.verifyRedis(key, value);
        return result;

    }
    @GetMapping("/updatePwd")
    @Operation(summary = "修改密码", description = "修改密码")
    public Result updatePwd(@RequestParam String email,@RequestParam String password) throws Exception {
        int res = userService.updatePwd(email, password);
        return Result.success(res);

    }
    @GetMapping("/isExitEmail")
    @Operation(summary = "查询邮箱是否已经存在注册", description = "查询邮箱是否已经存在注册")
    public Result isExitEmail(@RequestParam String email){
        int res = userService.selectEmail(email);
        return Result.success(res);
    }

}
